﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Noris.Tools.FrxEditor.Components
{
    /// <summary>
    /// Třída která nabízí systémové dialogy a okna, zjednodušuje jejich tvorbu a zobrazování
    /// </summary>
    public static class SystemDialogs
    {
        /// <summary>
        /// Vrátí okno pro dialog OpenFile.
        /// Okno není otevřené, lze jej donastavit a otevřít.
        /// </summary>
        /// <param name="title">Titulek dialogového okna</param>
        /// <param name="filter">String popisující filtr</param>
        /// <param name="initialDir">Výchozí adresář</param>
        /// <param name="multiSelect">Možnost multiselectu</param>
        /// <returns>Dialogové okno třídy OpenFileDialog</returns>
        public static OpenFileDialog GetOpenFileDialog(string title, string filter, string initialDir, bool multiSelect)
        {
            return GetOpenFileDialog(title, filter, 0, initialDir, multiSelect);
        }
        /// <summary>
        /// Vrátí okno pro dialog OpenFile.
        /// Okno není otevřené, lze jej donastavit a otevřít.
        /// </summary>
        /// <param name="title">Titulek dialogového okna</param>
        /// <param name="filter">String popisující filtr</param>
        /// <param name="filterIndex">Index aktuálně vybraného filtru, base 0</param>
        /// <param name="initialDir">Výchozí adresář</param>
        /// <param name="multiSelect">Možnost multiselectu</param>
        /// <returns>Dialogové okno třídy OpenFileDialog</returns>
        public static OpenFileDialog GetOpenFileDialog(string title, string filter, int filterIndex, string initialDir, bool multiSelect)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.AddExtension = false;
            ofd.AutoUpgradeEnabled = true;
            ofd.DefaultExt = "";
            ofd.DereferenceLinks = true;
            ofd.Filter = filter;
            ofd.FilterIndex = filterIndex;
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.Multiselect = multiSelect;
            ofd.InitialDirectory = initialDir;
            ofd.RestoreDirectory = true;
            ofd.SupportMultiDottedExtensions = true;
            ofd.Title = title;
            ofd.ValidateNames = false;

            return ofd;
        }
        /// <summary>
        /// Vrátí okno pro dialog SaveFile
        /// </summary>
        /// <param name="title"></param>
        /// <param name="filter"></param>
        /// <param name="initialDir"></param>
        /// <param name="initialFile"></param>
        /// <param name="addExt"></param>
        /// <returns></returns>
        public static SaveFileDialog GetSaveFileDialog(string title, string filter, string initialDir, string initialFile, string addExt)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.AddExtension = (!String.IsNullOrEmpty(addExt));
            sfd.AutoUpgradeEnabled = true;
            sfd.CreatePrompt = false;
            sfd.DefaultExt = addExt;
            sfd.FileName = initialFile;
            sfd.Filter = filter;
            sfd.FilterIndex = 0;
            sfd.CheckPathExists = true;
            sfd.InitialDirectory = initialDir;
            sfd.OverwritePrompt = true;
            sfd.RestoreDirectory = true;
            sfd.SupportMultiDottedExtensions = true;
            sfd.Title = title;
            sfd.ValidateNames = false;

            return sfd;
        }
        /// <summary>
        /// Metoda hledá adresář daného jména (searchFor) počínaje od daného adresáře (initialDir) nahoru a do stran.
        /// </summary>
        /// <param name="initialDir"></param>
        /// <param name="searchFor"></param>
        /// <returns></returns>
        public static string SearchNearestPath(string initialDir, string searchFor)
        {
            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(initialDir);
            while (dirInfo != null && dirInfo.Exists)
            {
                if (String.Equals(dirInfo.Name, searchFor, StringComparison.InvariantCultureIgnoreCase))
                    return dirInfo.FullName;

                System.IO.DirectoryInfo[] otherDirInfos = dirInfo.GetDirectories();
                System.IO.DirectoryInfo otherDirInfo = otherDirInfos.FirstOrDefault(di => String.Equals(di.Name, searchFor, StringComparison.InvariantCultureIgnoreCase));
                if (otherDirInfo != null)
                    return otherDirInfo.FullName;

                dirInfo = dirInfo.Parent;
            }
            return null;
        }
    }
}
